#include "mysqlite.h"
#include  <QSqlDatabase>
#include <QApplication>
#include <QSqlQuery>
mySqlite::mySqlite()
{
}

bool mySqlite::initSql()
{
      if(!connectDB())
          return false;
//      else if (!createTable())
//      {
//          return false;
//      }
      return true;
}

bool mySqlite::createTable()
{
  /*
    query=new QSqlQuery;
    QString str = "create table if not exists user("
                      "[id] integer primary key autoincrement,"
                      "[name] varchar(30),"
                      "[password] varchar(30)"
                      ")";

   QString inputGoods = "create table if not exists supplier("
                              "[id] integer primary key autoincrement,"
                              "[name] varchar(30),"//名称
                              "[date] varchar(8),"//时间
                              "[goodsPrice] integer,"//商品进价
                              "[goodsCount] integer,"//商品数量
                              "[goodsTotal] float" //商品总价
                              ")";
   QString outputGoods = "create table if not exists outputGoods("
                              "[id] integer primary key autoincrement,"
                              "[name] varchar(30),"
                              "[date] varchar(8),"//时间
                              "[client] varchar(30),"//客户
                              "[goodsPrice] integer,"//商品售价
                              "[goodsCount] integer,"//商品数量
                              "[goodsTotal] integer," //商品总价
                              "[finalPay] varchar(3 )"
                              ")";
    QString stock = "create table if not exists stock("
                              "[id] integer primary key autoincrement,"
                              "[name] varchar(30),"
                              "[goodsCount] integer,"//商品数量
                              "[goodsAlarm] integer" //商品总价
                              ")";
        if(! (query->exec(str) && query->exec(inputGoods) && query->exec(outputGoods) && query->exec(stock)))
            return false;
            */
        return true;
}

bool mySqlite::registerUser(QString name,QString password,QString role)
{
    query=new QSqlQuery;
    QString str = QString("insert into user values(null,'%1','%2','%3')").arg(name).arg(password).arg(role);
    if(!query->exec(str))
        return false;
    return true;
}


bool mySqlite::connectDB()
{
    QSqlDatabase myDB = QSqlDatabase::addDatabase("QSQLITE");
    myDB.setDatabaseName(QApplication::applicationDirPath() + "/Database/"+"myWMS.db");
    if(!myDB.open())
        return false;
    return true;
}


